﻿using Microsoft.Practices.Unity;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Ymca.DataAccess;
using Ymca.Entities;
using Ymca.Infrastrucure;
using Ymca.Web.Services.Interfaces;
using Ymca.Web.Models.Blog;

namespace Ymca.Web.Controllers
{
    public class BlogController : Controller
    {
		private IUnityContainer _container;
		private IPostsService _postsService;
		private ISearchService _searchService;

		public BlogController()
		{
			_container = ContainerConfig.Container;
			_postsService = _container.Resolve<IPostsService>();
			_searchService = _container.Resolve<ISearchService>();
		}

		[HttpGet]
        public ActionResult Index()
        {
			IndexViewModel viewModel = new IndexViewModel();
			viewModel.Posts = _postsService.GetPosts();
			return View(viewModel);
        }

		[HttpGet]
		public ActionResult Search(string search = "")
		{
			SearchViewModel viewModel = new SearchViewModel();
			viewModel.Page = 1;
			viewModel.Search = search;
			viewModel.Posts = new PagedList<Post>(_searchService.Search(search), viewModel.Page, Constants.PgeSize);
			return View(viewModel);
		}

		[HttpGet]
		[Authorize(Roles = "admin")]
		public ActionResult Create()
		{
			return View();
		}

		[HttpPost]
		[ValidateInput(false)]
		[Authorize(Roles = "admin")]
		public ActionResult Create(CreatePostViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				_postsService.AddPost(viewModel);
				return RedirectToAction("Index");
			}
			return View(viewModel);
		}

		[HttpGet]
		[Authorize(Roles = "admin")]
		public ActionResult Delete(int id = 0)
		{
			Post post = _postsService.GetPost(id);
			if (post != null)
			{
				DeletePostViewModel viewModel = new DeletePostViewModel();
				viewModel.Id = post.Id;
				viewModel.Date = post.Date;
				viewModel.Title = post.Title;
				return View(viewModel);
			}
			return HttpNotFound();
		}

		[HttpPost]
		[ActionName("Delete")]
		[Authorize(Roles = "admin")]
		public ActionResult DeleteConfirmed(int id)
		{
			if (_postsService.DeletePost(id))
			{
				return RedirectToAction("Index");
			}
			return HttpNotFound();
		}

		[HttpGet]
		public ActionResult Details(int id)
		{
			Post post = _postsService.GetPost(id);
			PostDetailsViewModel viewModel = new PostDetailsViewModel()
			{
				//Id = id,
				//Date = post.Date,
				//Title = post.Title,
				//Body = post.Body,
				//Attachments = post.Attachments != null ? post.Attachments : new List<Attachment>(),
				//Comments = post.Comments != null ? post.Comments : new List<Comment>()
			};

			return View(viewModel);
		}

		[HttpPost]
		[Authorize]
		public ActionResult Details(int? id, string comment)
		{
			if (ModelState.IsValid)
			{
				_postsService.AddComment(id.Value, User.Identity.Name, comment);
				return RedirectToAction("Details", new { id = id.Value });
			}
			return View();
		}
    }
}
